<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose
    title="批量更新"
    :showCancelBtn="true"
    :showOkBtn="true"
    :canFullscreen="false"
    :centered="true"
    :maskClosable="false"
    :width="750"
    wrapClassName="policy"
    @ok="handleOk"
  >
    <BasicForm @register="registerForm" :model="modelRef" />
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formBatch } from '../Policy.data';
import { one, updateBatch } from '../Policy.api';
import { customerSelect } from '/@/views/good/product/Product.api';
// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const customerOptions = ref([]);
//表单所有值，外部可更新
const modelRef = ref<any>({});

type RangeValue = [string, string];

//表单配置
const [registerForm, { resetFields, setFieldsValue, validate, updateSchema, clearValidate }] = useForm({
  labelWidth: 100,
  schemas: formBatch,
  showActionButtonGroup: false,
});

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  modelRef.value = {};
  customerOptions.value = await customerSelect()

  updateSchema([
    {
      field: "cId",
      componentProps: { options: customerOptions },
    },
    {
      field: "effectDate1",
      componentProps: { 
        onChange: onChange1,
      },
    },
    {
      field: "effectDate2",
      componentProps: { 
        onChange: onChange2,
      },
    },
  ])

  data.record.effectDate1 = [data.record.beginDate, data.record.endDate];
  data.record.effectDate2 = [data.record.beginDate, data.record.endDate];
  data.record.effectSdate = data.record.beginDate;
  data.record.effectEdate = data.record.endDate;
  data.record.customerId  = data.record.cId.join(",");
  
  // 无论新增还是编辑，都可以设置表单值
  if (typeof data.record === 'object') {
    setFieldsValue({
      ...data.record,
    });
  }

  await clearValidate();
});

const onChange1 = (val: RangeValue) => {
  var sDate = '', eDate = '';
  if (val && val != null && val.length > 1) {
    sDate = val[0];
    eDate = val[1];
  } 

  // modelRef.value = {
  //   beginDate: sDate,
  //   endDate: eDate
  // }
};

const onChange2 = (val: RangeValue) => {
  var sDate = '', eDate = '';
  if (val && val != null && val.length > 1) {
    sDate = val[0];
    eDate = val[1];
  } 

  modelRef.value = {
    effectSdate: sDate,
    effectEdate: eDate
  }
};

//表单提交事件
async function handleOk() {
  try {
    let values = await validate()

    setModalProps({ confirmLoading: true });
    //提交表单
    await updateBatch(values);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success', { isUpdate: isUpdate.value, values });
  } catch (error) {
    console.warn(error);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>

<style lang="less" scoped>
</style>